package org.makcro.sms.services.chat.base.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;

import org.makcro.sms.services.chat.model.Agent;
import org.makcro.sms.services.chat.model.Group;
import org.makcro.sms.services.chat.model.Question;

public class AgentDAO extends AbstractDAO<Agent> implements AgentDao {

	@PersistenceContext
	private EntityManager em;

	public AgentDAO() {
		super(Agent.class);
	}

	/* (non-Javadoc)
	 * @see org.makcro.sms.services.chat.base.dao.AgentDao#login(java.lang.String, java.lang.String)
	 */
	@Override
	public Agent login(String username, String password) {
		Agent agent = null;
		try {
			agent = (Agent) em.createNamedQuery("Agent.findByUsername").setParameter("username", username).setParameter("password", password).getSingleResult();
		} catch (NoResultException e) {

		}

		return agent;

	}

	@SuppressWarnings("unchecked")
	public List<Agent> selectAll() {
		List<Agent> agents = null;
		try {
			agents = (List<Agent>) em.createNamedQuery("Agent.findAll").getResultList();
		} catch (NoResultException e) {

		}

		return agents;
	}

	@SuppressWarnings("unchecked")
	public List<Question> findByAgent(Agent agent) {
		return em.createNamedQuery("Question.findByAgent").setParameter("agent", agent).getResultList();
	}

	@SuppressWarnings("unchecked")
	public List<Agent> findAgentByGroup(Group group) {
		List<Agent> agents = null;
		agents = (List<Agent>) em.createNamedQuery("Agent.findByGroup").setParameter("group", group).getResultList();
		return agents;
	}
}
